package com.zzzzzz.account.dashboard;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.zzzzzz.account.user.UserDao;
import com.zzzzzz.common.page.PageStart;
import com.zzzzzz.content.content.ContentListView;
import com.zzzzzz.content.content.ContentService;
import com.zzzzzz.core.web.BaseController;

@Controller
public class DashboardController extends BaseController {

	@Resource
	private ContentService contentService;
	@Resource
	private UserDao userDao;
	
	@RequestMapping(value="/u/{website}", method=RequestMethod.GET)
	public String view(@PathVariable String website, @RequestParam(value = "start", defaultValue = "0", required = false) Integer start, HttpServletRequest request, Model model) throws Exception {
		
		List<ContentListView> contentList = contentService.find("add_dt desc", start, PageStart.DEFAULT_OFFSET);
		Long contentCount = contentService.findCount();
		PageStart pageStart = new PageStart(start, PageStart.DEFAULT_OFFSET, contentCount, "/u/"+website);
		
		for(ContentListView contentListView : contentList){
			String picId = contentListView.getPicId();
			if(StringUtils.isNotBlank(picId)){
				String[] picIds = StringUtils.split(picId, ",");
				String[] picIdsShow = null;
				if(picIds.length >= 3){
					picIdsShow = new String[3];
					picIdsShow[0] = picIds[0];
					picIdsShow[1] = picIds[1];
					picIdsShow[2] = picIds[2];
				}else if(picIds.length < 3){
					picIdsShow = new String[1];
					picIdsShow[0] = picIds[0];
				}
				
				contentListView.setPicIds(picIdsShow);
				contentListView.setPicIdsCount(picIdsShow.length);
			}
		}
		
		model.addAttribute("contentList", contentList);
		model.addAttribute("count", contentCount);
		model.addAttribute("pageBar", pageStart.getPageBar());
		
		return "account/dashboard";
	}
	
	private static final Logger logger = LoggerFactory.getLogger(DashboardController.class);
}
